﻿using System;
using System.Collections.Generic;

namespace Candy.Infrastructure
{
    public static class ProgressHandler
    {
        private static object _locker = new object();

        private static List<object> _reporter = new List<object>();

        public static void ShowProgress(object reporter)
        {
            if (_progressControl == null)
                throw new InvalidOperationException("Progress handler is not initialized yet.");
            lock (_locker)
            {
                _reporter.Add(reporter);
            }
            _progressControl.Show();
        }


        public static void HideProgress(object reporter)
        {
            if (_progressControl == null)
                throw new InvalidOperationException("Progress handler is not initialized yet.");
            lock (_locker)
            {
                _reporter.Remove(reporter);
            }
            if (_reporter.Count == 0)
                _progressControl.Hide();
        }

        private static IProgressReporterControl _progressControl;
        public static void Initialize(IProgressReporterControl progressControl)
        {
            if (progressControl == null)
                throw new ArgumentNullException("ProgressReporterControl  can't be null.");
            _progressControl = progressControl;
        }
    }
}
